#include "boolproxy.h"

#include "../utils/expressionengine.h"

#include <QtScript/QScriptEngine>

using namespace core;
using namespace wfnet;

const QString BoolProxy::NAME = BoolProxy::staticMetaObject.className();

BoolProxy::BoolProxy(QObject* parent) : AbstractTypeProxy(parent),
    m_value(Bool())
{

}

BoolProxy::BoolProxy(const BoolProxy& proxy) : AbstractTypeProxy(proxy.parent()),
    m_value(proxy.m_value)
{
    m_engine = proxy.m_engine;
}

BoolProxy::~BoolProxy()
{

}

void BoolProxy::setValue(const QVariant& value)
{
    m_value.setValue(value);
}

const core::Type* BoolProxy::getValue() const
{
    return &m_value;
}

QScriptValue BoolProxy::equal(const QScriptValue& other)
{
    Q_ASSERT(m_engine != 0);

    BoolProxy* _other = qobject_cast<BoolProxy* >(other.toQObject());

    BoolProxy* ret = new BoolProxy();
    ExpressionEngine::getInstance()->registerProxyInstance(ret);
    ret->setEngine(m_engine);

    if (_other != 0)
    {
        const bool value = m_value == _other->m_value;
        ret->setValue(value);
    }
    else
    {
        ret->setValue(false);
    }

    return m_engine->newQObject(ret);
}

QScriptValue BoolProxy::notEqual(const QScriptValue& other)
{
    Q_ASSERT(m_engine != 0);

    BoolProxy* _other = qobject_cast<BoolProxy* >(other.toQObject());

    BoolProxy* ret = new BoolProxy();
    ExpressionEngine::getInstance()->registerProxyInstance(ret);
    ret->setEngine(m_engine);

    if (_other != 0)
    {
        const bool value = m_value != _other->m_value;
        ret->setValue(value);
    }
    else
    {
        ret->setValue(true);
    }

    return m_engine->newQObject(ret);
}
